<?php

class ContestantTable extends Doctrine_Table
{
  public function getBackendListQuery(Doctrine_Query $query)
  {
    $rootAlias = $query->getRootAlias();
    
    $user = sfContext::getInstance()->getUser();

    $query->innerJoin("$rootAlias.area e");
    
    if ($user->hasCredential('admin'))
    {    
      $query->andWhereIn("$rootAlias.area_id", $user->getAreaIds());
    }

    return $query;
  }

  public function getAvailableQuery()
  {
    return $this->createQuery('c')
      ->where('number <> 0')
      ->orderBy('c.hits desc'); // default
  }
  
  public function getAreaPollFormChoiceQuery()
  {
    $area = Doctrine::getTable('Area')->findOneBySlug(
      sfContext::getInstance()->getRequest()->getParameter('area')
    );
    
    return $area->getContestantsQuery();
  }
}
